package com.mju.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mju.pojo.Cart;
import com.mju.vo.CartVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CartDao extends BaseMapper<Cart> {
    /**
     * 插入购物车数据，用已经mp提供给我们的insert()
     */
    /**
     * 修改购物车数据中的商品的数量，也用mp提供给我们的updateById()
     */
    /**
     * 根据用户id和商品id查询购物车中的数据
     * @param uid 用户id
     * @param pid 商品id
     * @return
     */
    @Select("select * from t_cart where uid=#{uid} AND pid=#{pid}")
    Cart selectByUidAndPid(@Param("uid") Integer uid, @Param("pid") Integer pid);

    /**
     * 根据购物车表和商品表联合查询数据
     * @param uid 用户id
     * @return
     */
    @Select("select cid,uid,pid,t_cart.price,t_cart.num,title,t_product.price as realPrice,image " +
            "from t_cart left join t_product on t_cart.pid = t_product.id where uid=#{uid} order by t_cart.created_time desc")
    List<CartVO> selectVOByUid(Integer uid);

    // 根据cids查询购物车数据
    @Select({"<script>select cid,uid,pid,t_cart.price,t_cart.num,title,t_product.price as realPrice,image " +
            "from t_cart left join t_product on t_cart.pid = t_product.id where" +
            " cid in ( <foreach collection='array' item='item' separator=','> #{item} </foreach>) " +
            " order by t_cart.created_time desc </script>"})
    List<CartVO> selectVOByCids(Integer[] cids);


    // 根据pid获取cid
    @Select("select cid from t_cart where pid=#{pid}")
    Integer getCidByPid(Integer pid);
}
